package ui;

import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Work.*;
/*管理员增删改*/
public class LibrarianOther extends JFrame implements ActionListener {
	JTextField jtf1, jtf2, jtf3;
	private JButton updatel_name;
	private JButton updatel_number;
	private JButton returnbutton;

	//创建数据库连接类对象
    Jdbc db = new Jdbc();
	// 添加管理员
	public void AddLibrarian() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("添加管理员"));
		JLabel jl2 = new JLabel("管理员姓名:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("管理员电话:");
		jtf3 = new JTextField(10);
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("add");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("管理员添加界面");
		setSize(280, 155);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 删除管理员
	public void DeleteLibrarian() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(3, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("删除管理员（任选其一输入）"));
		JLabel jl1 = new JLabel("管理员编号:");
		jtf1 = new JTextField(10);
		JLabel jl2 = new JLabel("管理员姓名:");
		jtf2 = new JTextField(10);
		JLabel jl3 = new JLabel("管理员电话:");
		jtf3 = new JTextField(10);
		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf1);// 在面板jp1上添加输入框
		jp1.add(jl2);
		jp1.add(jtf2);
		jp1.add(jl3);
		jp1.add(jtf3);
		jpc.add(jp1);// 把面板jp1加到流式布局上
		add(jpc);// 添加流式布局
		// add(jpc, BorderLayout.CENTER);

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("delete");// 没看懂

		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("管理员删除界面");
		setSize(280, 185);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	// 修改管理员
	public void UpdateLibrarianButton() {
		JFrame jFrame = new JFrame("修改部分选择");
		jFrame.setLayout(new BorderLayout());
		JPanel jp1 = new JPanel(new GridLayout(1, 2, 10, 10));// 设置新面板
		jp1.setBorder(BorderFactory.createTitledBorder("管理员修改"));
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		setTitle("管理员修改系统功能选项");
		setSize(320, 200);
		setLocationRelativeTo(null);

		// 监听右上角的退出按钮
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});
		updatel_name = new JButton("修改管理员姓名");
		updatel_number = new JButton("修改管理员电话");

		jp1.add(updatel_name);
		jp1.add(updatel_number);
		returnbutton = new JButton("返回");
		returnbutton.addActionListener(this);
		returnbutton.setActionCommand("no");
		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(returnbutton);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方
		getContentPane().add(jp1, BorderLayout.CENTER);
		updatel_name.addActionListener(this);
		updatel_number.addActionListener(this);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		LibrarianOther librarianother = new LibrarianOther();
		LibrarianOther1 librarianother1 = new LibrarianOther1();
		ForeignKey foreignkey = new ForeignKey();
        ChangeLibrarian changelibrarian = new ChangeLibrarian();
        ArrayList<Librarian> librarianlist = changelibrarian.read(db);
		if (e.getSource() == updatel_name) {//修改
			this.dispose();
			librarianother1.Updatel_name();
		}else if (e.getSource() == updatel_number) {
			this.dispose();
			librarianother1.Updatel_number();
		}else if (e.getActionCommand().equals("add")) {//增加
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			if ( s2.equals("") || s3.equals("")) {
				JOptionPane.showMessageDialog(this, "内容缺失，请检查是否填写完毕全部信息");
			} else {
				db.uiinsertlibrarian(db,s2,s3);
				JOptionPane.showMessageDialog(this, "管理员姓名:" + s2 +" 管理员电话:" + s3 + "\n管理员信息添加成功");
				this.dispose();
			}
		}else if (e.getActionCommand().equals("delete")) {//删除
			String s1 = jtf1.getText().trim();
			String s2 = jtf2.getText().trim();
			String s3 = jtf3.getText().trim();
			if (s2.equals("") && s3.equals("")) {
				if (!foreignkey.FindL_ID(librarianlist, s1)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					librarianother.DeleteLibrarian();
				}
				db.uideletelibrarian(db, s1, 1);
				JOptionPane.showMessageDialog(this, "管理员编号为 " + s1 + " 的管理员信息已删除");
				this.dispose();
			} else if (s1.equals("") && s3.equals("")) {
				if (!foreignkey.FindL_NAME(librarianlist, s2)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					librarianother.DeleteLibrarian();
				}
				db.uideletelibrarian(db, s2, 2);
				JOptionPane.showMessageDialog(this, "管理员姓名为 " + s2 + " 的管理员信息已删除");
				this.dispose();
			} else if (s1.equals("") && s2.equals("")) {
				if (!foreignkey.FindL_NUMBER(librarianlist, s3)) {
					JOptionPane.showMessageDialog(this, "查无此人，请重新输入！");
					librarianother.DeleteLibrarian();
				}
				db.uideletelibrarian(db, s3, 3);
				JOptionPane.showMessageDialog(this, "管理员电话为 " + s3 + " 的管理员信息已删除");
				this.dispose();
			}
		}else if (e.getSource() == returnbutton) {
			this.dispose();
			new LibrarianOp();
		} else {
			jtf1.setText("");
			jtf2.setText("");
			jtf3.setText("");
		}

	}
}
